<?php 

/** DAO de Campus */
class DaoCampus extends DAO {
	
	public function get($id) {
		$select = db_select(Campus::TYPE)
		->fields(Campus::TYPE)
		->condition('id', $id)
		->execute()
		->fetchAll();
		
		$campus = new Campus();
		foreach($select as $row) {			
			$campus->id 					= $row->id;
			$campus->nome 				= $row->nome;
			$campus->restricoes		= $this->getRestricoes($campus->id);
		}
		
		return $campus;
	}
	
	/** Carrega varias instancias do banco */
	public function listar() {
		$select = db_select(Campus::TYPE)
								->fields(Campus::TYPE)
									->execute()
										->fetchAll();
		
		$campi = array();
		foreach($select as $row) {
			$campus 							= new Campus();
			$campus->id 					= $row->id;
			$campus->nome 				= $row->nome;
			$campus->restricoes		= $this->getRestricoes($campus->id);
					
			array_push($campi, $campus);
		}
		
		return $campi;
	}
	
	/** Funcao que carrega as restricoes de um determinado campus */
	public function getRestricoes($campus_id) {
		$select = db_select('restricao_campus', 'r')
								->fields('r')
									->condition('campus_id', $campus_id)
										->execute()
											->fetchAll();
		
		$modalidades_restritas = array();
		foreach($select as $row) {
			$modalidade = DAO::daoFactory(Modalidade::TYPE)->get($row->modalidade_id);
			array_push($modalidades_restritas, $modalidade);
		}
		
		return $modalidades_restritas;
	}
	
}














